package com.example.team.dao.Imp;

import com.example.team.dao.IOrderDao;
import com.example.team.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *根据接口操作数据库表的接口规范，对其进行实现
 * 编写者：蓝江富
 * 2023年10月30日21:24:32
 */
@Repository
public class IOrderDaoImpl implements IOrderDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertOrder(Order order) {
        return jdbcTemplate.update(
                "insert into orders(" +
                        "orderId," +
                        "total," +
                        "orderDate," +
                        "tel," +
                        "applicant" +
                        ") " +
                        "values(?,?,?,?,?)"
                ,order.getOrderId()
                ,order.getTotal()
                ,order.getOrderDate()
                ,order.getTel()
                ,order.getApplicant()
        );
    }

    @Override
    public int deleteOrder(Order order) {
        return jdbcTemplate.update(
                "delete from orders " +
                "where orderId=?",order.getOrderId());
    }

    @Override
    public int updateOrder(Order order) {
        //如果更新的字段为空，则保留旧数据
        Order order1=getOrderById(order);
        if(order.getTotal()==null)
            order.setTotal(order1.getTotal());
        if(order.getOrderDate()==null)
            order.setOrderDate(order1.getOrderDate());
        if(order.getTel()==null)
            order.setTel(order1.getTel());
        if(order.getApplicant()==null)
            order.setApplicant(order1.getApplicant());
        return jdbcTemplate.update("update orders set " +
                        "" +
                        "total=?," +
                        "orderDate=?," +
                        "tel=?," +
                        "applicant=? " +
                        "where orderId=?"
                ,order.getTotal()
                ,order.getOrderDate()
                ,order.getTel()
                ,order.getApplicant()
                ,order.getOrderId()
        );
    }

    @Override
    //根据申请人姓名查询
    public List<Order> getAllOrder(Order order) {
        if(order.getApplicant()!=null)
            return jdbcTemplate.query("select " +
                            "orderId," +
                            "total," +
                            "orderDate," +
                            "tel," +
                            "applicant " +
                "from orders where applicant='"+order.getApplicant()+"'",new Object[]{},
                new BeanPropertyRowMapper<>(Order.class));
        else
            return jdbcTemplate.query("select " +
                            "orderId," +
                            "total," +
                            "orderDate," +
                            "tel," +
                            "applicant " +
                "from orders",new Object[]{},
                new BeanPropertyRowMapper<>(Order.class));
    }

    @Override
    public Order getOrder(Order order) {
        List<Order> result=jdbcTemplate.query("select " +
                        "orderId," +
                        "total," +
                        "orderDate," +
                        "tel," +
                        "applicant " +
                                "from orders where applicant='"+order.getApplicant()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(Order.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }

    @Override
    public Order getOrderById(Order order) {
        List<Order> result=jdbcTemplate.query("select " +
                        "orderId," +
                        "total," +
                        "orderDate," +
                        "tel," +
                        "applicant " +
                                "from orders where orderId='"+order.getOrderId()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(Order.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
